package com.friend.campusnotice;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.friend.campusnotice.adapter.PickListAdapter;
import com.friend.campusnotice.bean.Contacts;
import com.friend.campusnotice.util.MyUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PcikContactsActivity extends AppCompatActivity {

    private RecyclerView recyleView;
    private List<Contacts> contactsList;
    private Map<String, Object> myData;
    private PickListAdapter pickListAdapter;
    private Toolbar toolbar;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            pickListAdapter.notifyDataSetChanged();
            // Toast.makeText(MainActivity.this,"加载完了",Toast.LENGTH_LONG).show();

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_pcik_contacts);
        initData();
        initView();
    }

    private void initView() {
        contactsList = new ArrayList<Contacts>();
        recyleView = (RecyclerView) findViewById(R.id.swipe_target);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle("选择联系人");

      // toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_48dp);

        pickListAdapter = new PickListAdapter(PcikContactsActivity.this,contactsList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(PcikContactsActivity.this);
        recyleView.setLayoutManager(layoutManager);
        recyleView.setAdapter(pickListAdapter);

    }


    private void initData() {

        new Thread() {
            @Override
            public void run() {
                contactsList.clear();
                MyUtil myUtil = new MyUtil();
                myData = myUtil.getPhoneContacts(PcikContactsActivity.this);
                ArrayList<String> name = (ArrayList<String>) myData.get("ContactsName");
                ArrayList<String> number = (ArrayList<String>) myData.get("ContactsNumber");
                ArrayList<Long> ContactId = (ArrayList<Long>) myData.get("ContactId");
                ArrayList<Long> ContactIconId = (ArrayList<Long>) myData.get("ContactsIconId");
                for (int i = 0; i < name.size(); i++) {
                    Contacts contact = new Contacts();
                    contact.setContactName(name.get(i));
                    contact.setPhoneNumber(number.get(i));
                    contact.setContact_Id(ContactId.get(i));
                    contact.setPhotoid(ContactIconId.get(i));
                    //contact.setUserHead(phonto.get(i));
                    contactsList.add(contact);
                    handler.sendEmptyMessage(0);
                }
            }
        }.start();


    }

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
       // return super.onCreatePanelMenu(featureId, menu);
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.menu_commit:
                 Toast.makeText(PcikContactsActivity.this,"确定",Toast.LENGTH_LONG).show();
                 getSelectedItem();
                break;
            case R.id.menu_cacel:
                Toast.makeText(PcikContactsActivity.this,"取消",Toast.LENGTH_LONG).show();
                finish();
                break;
        }


        return super.onOptionsItemSelected(item);
    }

    private void getSelectedItem() {

       ArrayList<String> items = (ArrayList<String>) pickListAdapter.getPickItems();


        Intent intent = getIntent();
        intent.putStringArrayListExtra("items",items);
        setResult(2, intent);
        finish();
    }
}
